﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using EFFC.Net.Interface;
using EFFC.Net.Parameters;
using EFFC.Net.Common;
using EFFC.Net.ResourceLib.Parameters;

namespace EFFC.Net.ResourceLib.Communication
{
   
    public class EmailSents : IResourceEntity
    {
        string _id = "";
        #region IResourceEntity Members

        public string ID
        {
            get
            {
                if (_id == "")
                {
                    _id = "SentEmail" + Guid.NewGuid().ToString();
                }

                return _id;
            }
        }

        public void Release()
        {
            
        }

        #endregion

        public bool SendMailSmtp(EmailParameter p)
        {
            MailMessage msg = null;
            SmtpClient objSmtpClient = null;
            try
            {
                msg = new MailMessage();
                msg.From = new MailAddress(p.From);
                //收件人地址以逗號分隔
                foreach (string s in p.To.Replace(",", ";").Split(';'))
                {
                    msg.To.Add(s);
                }
                msg.Subject = p.Subject;
                msg.SubjectEncoding = p.BodyEncoding;
                msg.Body = p.Body;
                msg.BodyEncoding = p.BodyEncoding;
                msg.IsBodyHtml = p.IsBodyHTML;
                msg.Priority = MailPriority.High;
                if (p.Attachments.Count > 0)
                {
                    foreach (Attachment atta in p.Attachments)
                    {
                        msg.Attachments.Add(atta);
                    }
                }
                ///以下四個自定義Header是將郵件偽裝成OutLook發送的。
                ///目的是為了防止一些網站的反垃圾郵件功能
                ///將本系統發送的郵件當做垃圾郵件過濾掉了
                msg.Headers.Add("X-Priority", "3");
                msg.Headers.Add("X-MSMail-Priority", "Normal");
                msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
                msg.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");

                //msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 
                //msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", p.User); //set your username here 
                //msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p.Pass); //set your password here


                objSmtpClient = new SmtpClient();
                objSmtpClient.Host = p.smtpServer;
                objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                objSmtpClient.Credentials = new System.Net.NetworkCredential(p.User, p.Pass);

                objSmtpClient.Send(msg);
                return true;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return false;
            }
            finally
            {
                if (msg != null)
                {
                    msg.Dispose();
                }
                if (objSmtpClient != null)
                {
                    objSmtpClient.Dispose();
                }
            }
        }
    }
}
